#include "stdafx.h"
#include "RenderBuffer.h"

using namespace GxLibBasic;

namespace RayTracePro
{
	void RenderBuffer::SavePFM(const wchar_t * fileName)
	{
		float * pixels = new float[Width*Height*3];
		for (int i=0; i<Buffer.Count(); i++)
		{
			pixels[i*3] = Buffer[i].x;
			pixels[i*3+1] = Buffer[i].y;
			pixels[i*3+2] = Buffer[i].z;
		}
		FileStream stream(fileName, GxLibBasic::gxfmWrite);
		stream.WriteBuffer("PF\n", 3);
		String s(Width);
		stream.WriteBuffer(s.ToMBString(), s.GetLength());
		stream.WriteBuffer(" ", 1);
		s = String(Height);
		stream.WriteBuffer(s.ToMBString(), s.GetLength());
		stream.WriteBuffer("\n-1.000000\n", 11);
		for (int h = Height-1; h>=0; h--)
		{
			stream.WriteBuffer(&pixels[h*Width*3], Width*3);
			//stream.WriteBuffer("\n", 1);
		}
		delete [] pixels;
	}
}